//
//  DateTools.m
//  Birthday Reminder
//
//  Created by Andrei Marcu on 10/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DateTools.h"


@implementation DateTools

+ (NSDateComponents *)getDateComponentsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
	NSDateComponents *components = [gregorian components:unitFlags fromDate:fromDate toDate:toDate options:0];
    [gregorian release];
    return components;
}

+ (NSDateComponents *)getDateComponentsFromDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];
    return components;
}

+ (NSString *)getNumberOfYearsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSDateComponents *components = [DateTools getDateComponentsFromDate:fromDate toDate:toDate];
    NSInteger years = [components year];
    return [NSString stringWithFormat:@"%d", years];
}

+ (NSString *)getStringNumberOfDaysFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSDateComponents *components = [DateTools getDateComponentsFromDate:fromDate toDate:toDate];
    NSInteger days = [components day];
    return [NSString stringWithFormat:@"%d", days];

}

+ (NSString *)getStringNumberOfMonthsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSDateComponents *components = [DateTools getDateComponentsFromDate:fromDate toDate:toDate];
    NSInteger months = [components month];
    return [NSString stringWithFormat:@"%d", months];

}

+ (NSInteger)getDayComponentFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSDateComponents *components = [DateTools getDateComponentsFromDate:fromDate toDate:toDate];
    NSInteger days = [components day];
    return days;
    
}

+ (NSInteger)getMonthComponentFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSDateComponents *components = [DateTools getDateComponentsFromDate:fromDate toDate:toDate];
    NSInteger months = [components month];
    return months;
}

+ (NSInteger)getYearFromDate:(NSDate *)date {
    NSDateComponents *components = [DateTools getDateComponentsFromDate:date];
    return [components year];
}

+ (NSInteger)getMonthFromDate:(NSDate *)date {
    NSDateComponents *components = [DateTools getDateComponentsFromDate:date];
    return [components month];
}

+ (NSInteger)getDayFromDate:(NSDate *)date {
    NSDateComponents *components = [DateTools getDateComponentsFromDate:date];
    return [components day];
}

+ (NSString *)getStringMonthFromDate:(NSDate *)date {
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"MMMM"];
    NSString *month = [formater stringFromDate:date];
    [formater release];
    return month;
}

+ (NSInteger)getNumberOfDaysFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	unsigned int unitFlags = NSDayCalendarUnit;
	NSDateComponents *components = [gregorian components:unitFlags fromDate:fromDate toDate:toDate options:0];
    [gregorian release];
    return [components day];
}

+ (NSDate *)getNextBirthdayForDate:(NSDate *)date {
    NSDateComponents *birthdayDateComponents = [[NSDateComponents alloc] init];
    [birthdayDateComponents setYear:[DateTools getYearFromDate:[NSDate date]]];
    [birthdayDateComponents setMonth:[DateTools getMonthFromDate:date]];
    [birthdayDateComponents setDay:[DateTools getDayFromDate:date]];
    [birthdayDateComponents setHour:0];
    [birthdayDateComponents setMinute:2];
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [cal setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    NSDate *birthdateThisYear = nil;
    birthdateThisYear = [cal dateFromComponents:birthdayDateComponents];
    NSTimeInterval timeUntilBirthday = [birthdateThisYear timeIntervalSinceNow];
    NSDate *nextBirthday;
    if (timeUntilBirthday > 0) {
        nextBirthday = birthdateThisYear;
    } else {
        [birthdayDateComponents setYear:[DateTools getYearFromDate:[NSDate date]] + 1];
        nextBirthday = [cal dateFromComponents:birthdayDateComponents];
    }
    
    [birthdayDateComponents release];
    [cal release];
    
    return nextBirthday;
}

@end
